
import wx
from log_object import *

class ListPanel():
	def __init__(self, parent, mainPanel, title):
		self.mainPanel = mainPanel
		self.parent = parent
		self.isUserPanel = True 
		if title == "Case" :
			self.isUserPanel = False 
		self.__add_items(title)

	def __add_items(self, title):
		vbox_top = wx.BoxSizer(wx.VERTICAL)
		vbox = wx.BoxSizer(wx.VERTICAL)

		txt = wx.StaticText(self.mainPanel, -1, title + ':', (5, 5))
		vbox.Add(txt, 0, wx.BOTTOM, 3)

		width = 280
		self.inList = wx.ListBox(self.mainPanel, -1, (5,25), (width,300), "", style=wx.LB_HSCROLL|wx.LB_NEEDED_SB|wx.LB_EXTENDED)
		vbox.Add(self.inList, 0, wx.BOTTOM, 3)
		self.inList.SetForegroundColour(wx.BLACK)
		txt = wx.StaticText(self.mainPanel, -1, '* Double click to move.', (5, 330))
		vbox.Add(txt, 0, wx.BOTTOM, 3)

		txt = wx.StaticText(self.mainPanel, -1, 'Selected ' + title + ':', (5, 370))
		vbox.Add(txt, 0, wx.BOTTOM, 3)

		width = 280
		self.outList = wx.ListBox(self.mainPanel, -1, (5,390), (width,300), "", style=wx.LB_HSCROLL|wx.LB_NEEDED_SB|wx.LB_EXTENDED)
		vbox.Add(self.outList, 0, wx.BOTTOM, 3)
		self.outList.SetForegroundColour(wx.BLACK)

		updateBtn = wx.Button(self.mainPanel, -1, "Update", (5, 695), (100, 30))
		vbox.Add(updateBtn, 0, wx.TOP, 15)

		self.mainPanel.SetSizer(vbox_top)

                #self.mainPanel.Bind(wx.EVT_LISTBOX, self.__onAddto, self.inList)
                self.mainPanel.Bind(wx.EVT_LISTBOX_DCLICK, self.__onAddto, self.inList)
                self.mainPanel.Bind(wx.EVT_LISTBOX_DCLICK, self.__onRemove, self.outList)
		self.mainPanel.Bind(wx.EVT_BUTTON, self.__onUpdate, updateBtn)

	def  __onAddto(self, event):
		user_list = self.inList.GetSelections()
		idx = self.outList.GetCount()
		for user in user_list:
			selected_item = self.inList.GetString(user)
			for i in range(idx):
				user_title =  self.outList.GetString(i)
				if selected_item == user_title:
					return
			self.outList.InsertItems([selected_item], idx)
			if self.isUserPanel == True :
				self.parent.addParticipant(selected_item)
			else :
				self.parent.addCase(selected_item)
			idx += 1

	def  __onRemove(self, event):
		user_idx = self.outList.GetSelection()
		selected_item = self.outList.GetString(user_idx)
		if self.isUserPanel == True :
			self.parent.deleteParticipant(selected_item)
		else :
			self.parent.deleteCase(selected_item)
 		self.outList.Delete(user_idx)

	def __onUpdate(self, event):
		self.parent.updateDrawing()

	def clearPanel(self):
		self.inList.Clear()
		self.outList.Clear()

	def addInData(self, name):
		idx = self.inList.GetCount()
		found = False
		for i in range(idx) : 
			item = self.inList.GetString(i)
			if item == name : 
				found = True 
				break
		if found == False :
			self.inList.InsertItems([name], idx)

		#if self.isUserPanel == False :
		#	if self.outList.GetCount() == 0:
		#		self.outList.InsertItems([name], idx)
		#		self.parent.addCase(name)
			

		
class ScriptPanel():
	def __init__(self, parent, mainPanel):
		self.mainPanel = mainPanel
		self.parent = parent
		self.__add_items()

	def __add_items(self):
		vbox_top = wx.BoxSizer(wx.VERTICAL)
		vbox = wx.BoxSizer(wx.VERTICAL)

		width = 280
		panel1 = wx.Panel(self.mainPanel, -1, (5, 5), (width, 50))
		noteBtn = wx.RadioButton(panel1, -1, "Note", style=wx.RB_GROUP, pos=(0, 0))
		noteBtn.SetValue(True)
		scriptBtn = wx.RadioButton(panel1, -1, "Script", pos=(150,0))
		scriptBtn.SetValue(False)
		#scriptBtn.Enable(False)
		vbox.Add(panel1, 0, wx.TOP, 9)

		self.inList = wx.ListBox(self.mainPanel, -1, (5,30), (width, 400), "", style=wx.LB_HSCROLL|wx.LB_NEEDED_SB|wx.LB_EXTENDED)
		vbox.Add(self.inList, 0, wx.BOTTOM, 3)
		self.inList.SetForegroundColour(wx.BLACK)

		delBtn = wx.Button(self.mainPanel, -1, "Delete", (5, 430), (100, 30))
		#self.mainPanel.Bind(wx.EVT_BUTTON, self.OnCalcMatch, delBtn)
		vbox.Add(delBtn, 0, wx.TOP, 15)

		panel2 = wx.Panel(self.mainPanel, -1, (5, 490), (width, 30), style=wx.BORDER_SIMPLE)
		wx.StaticText(panel2, -1, 'New Note', (0, 5))
		vbox.Add(panel2, 0, wx.TOP, 9)

		panel3 = wx.Panel(self.mainPanel, -1, (5, 520), (width, 200))
		wx.StaticText(panel3, -1, 'Time:', (0, 5))
		self.time = wx.TextCtrl(panel3, -1, "", (50, 5), (150, 25), style=wx.SUNKEN_BORDER )
                #self.time.SetEditable(True)
		self.note = wx.TextCtrl(panel3, -1, "", (0, 35), (width, 100), style=wx.SUNKEN_BORDER )
		addBtn = wx.Button(panel3, -1, "Add", (5, 130), (100, 30))
		vbox.Add(panel3, 0, wx.TOP, 9)

		self.mainPanel.SetSizer(vbox_top)


class GraphPanel():
	def __init__(self, parent, mainPanel):
		self.mainPanel = mainPanel
		self.parent = parent
		self.__add_items()

	def __add_items(self):
		vbox_top = wx.BoxSizer(wx.VERTICAL)
		vbox = wx.BoxSizer(wx.VERTICAL)

		width =280
		# divide screen, .....
		panel1 = wx.Panel(self.mainPanel, -1, (5, 10), (width, 430), style=wx.BORDER_SIMPLE)
		self.test = wx.ComboBox(panel1, -1, "heatmap", (110,3), (150, 25), ("heatmap", "busy", "distance", "vector", "trend", "window", "space", "test"), wx.CB_DROPDOWN) 
		txt = wx.StaticText(panel1, -1, 'Graph Type:', (5, 5))
		self.graph = wx.ComboBox(panel1, -1, "heatmap", (110,15), (150, 25), ("heatmap", "busy", "distance", "vector", "trend", "window", "space", "test"), wx.CB_DROPDOWN) 
		txt = wx.StaticText(panel1, -1, 'Property 1:', (5, 35))
		self.property1 = wx.ComboBox(panel1, -1, "window-move", (110,32), (150, 25), ("pointer-move", "window-new", "window-move", "vector", "active", "click", "lifetime", "distri-lifetime", "size", "distri-size", "pointer-all", "pointer-color", "section", "section-setin"), wx.CB_DROPDOWN) 
		txt = wx.StaticText(panel1, -1, 'Property 2:', (5, 65))
		self.property2 = wx.ComboBox(panel1, -1, "none", (110, 63), (150, 25), ("none", "time", "pointer"), wx.CB_DROPDOWN) 
		self.property2.Enable(False)

		self.check = wx.CheckBox(panel1, -1, 'Set Same Configuration to All Space', pos=(5, 105))
                self.check.SetValue(False)
		addBtn = wx.Button(panel1, -1, "Add", (5, 125), (100, 30))

		self.inList = wx.ListBox(panel1, -1, (5,165), (width-10,200), "", style=wx.LB_HSCROLL|wx.LB_NEEDED_SB|wx.LB_EXTENDED)
		self.inList.SetForegroundColour(wx.BLACK)
		graphUpdateBtn = wx.Button(panel1, -1, "Update", (5, 370), (100, 30))
		delBtn = wx.Button(panel1, -1, "Delete", (160, 370), (100, 30))
		vbox.Add(panel1, 0, wx.TOP, 9)

		panel2 = wx.Panel(self.mainPanel, -1, (5, 460), (width, 140), style=wx.BORDER_SIMPLE)
		txt = wx.StaticText(panel2, -1, 'Time Range:', (5, 5))
		self.time_start = wx.TextCtrl(panel2, -1, "0", (100, 5), (70, 25), style=wx.SUNKEN_BORDER )
		txt = wx.StaticText(panel2, -1, '~', (175, 5))
		self.time_end = wx.TextCtrl(panel2, -1, "-1", (190, 5), (70, 25), style=wx.SUNKEN_BORDER )
		txt = wx.StaticText(panel2, -1, 'Active Range:', (5, 40))
		self.gap = wx.TextCtrl(panel2, -1, str(POINTER_ACTIVE_GAP), (100, 40), (70, 25), style=wx.SUNKEN_BORDER )
		txt = wx.StaticText(panel2, -1, 'Sec.', (175, 40))

		txt = wx.StaticText(panel2, -1, 'Effect Range:', (5, 75))
		self.effect = wx.TextCtrl(panel2, -1, str(EFFECTIVE_RANGE), (100, 75), (70, 25), style=wx.SUNKEN_BORDER )

		updateBtn = wx.Button(panel2, -1, "Update", (5, 100), (100, 30))
		vbox.Add(panel2, 0, wx.TOP, 9)

		panel3 = wx.Panel(self.mainPanel, -1, (5, 620), (width, 200), style=wx.BORDER_SIMPLE)
		txt = wx.StaticText(panel3, -1, 'Point:', (5, 5))
		self.point = wx.TextCtrl(panel3, -1, "0", (100, 5), (70, 25), style=wx.SUNKEN_BORDER )
		pAddBtn = wx.Button(panel3, -1, "Add", (5, 30), (100, 30))
		pDelBtn = wx.Button(panel3, -1, "Delete", (160, 30), (100, 30))
		self.pList = wx.ListBox(panel3, -1, (5,80), (width-10,110), "", style=wx.LB_HSCROLL|wx.LB_NEEDED_SB|wx.LB_EXTENDED)
		self.pList.SetForegroundColour(wx.BLACK)
		vbox.Add(panel3, 0, wx.TOP, 9)

		self.mainPanel.SetSizer(vbox_top)

		self.mainPanel.Bind(wx.EVT_BUTTON, self.__onAddSpaceGraph, addBtn)
		self.mainPanel.Bind(wx.EVT_BUTTON, self.__onTimeRange, updateBtn)
		self.mainPanel.Bind(wx.EVT_BUTTON, self.__onDeleteSpaceGraph, delBtn)
		self.mainPanel.Bind(wx.EVT_BUTTON, self.__onSetGraph, graphUpdateBtn)
		self.mainPanel.Bind(wx.EVT_BUTTON, self.__onAddTag, pAddBtn)
		self.mainPanel.Bind(wx.EVT_BUTTON, self.__onDeleteTag, pDelBtn)

	def clearPanel(self):
		self.time_start.SetValue("0")
		self.time_end.SetValue("-1")
		self.pList.Clear()

	def  __onAddTag(self, event):
		idx = self.pList.GetCount()
		name = self.point.GetValue()
		self.pList.InsertItems([name], idx)

	def  addTag(self, name):
		idx = self.pList.GetCount()
		self.pList.InsertItems([name], idx)

	def  __onDeleteTag(self, event):
		idx = self.pList.GetSelection()
		self.pList.Delete(idx)

	def  getTagList(self):
		l = []
		for i in range(self.pList.GetCount()) :
			item = self.pList.GetString(i)
			l.append(item)
		return l


	def  __onAddSpaceGraph(self, event):
		idx = self.inList.GetCount()
		name = self.graph.GetValue() + " " + self.property1.GetValue() + " " + self.property2.GetValue()
		self.inList.InsertItems([name], idx)

	def  __onDeleteSpaceGraph(self, event):
		idx = self.inList.GetSelection()
		self.inList.Delete(idx)

	def  __onTimeRange(self, event):
		start = int(self.time_start.GetValue())
		end = int(self.time_end.GetValue())
		gap = int(self.gap.GetValue())
		value= int(self.effect.GetValue())
		self.parent.setEffectRange(value)
		self.parent.setActiveTime(gap)
		self.parent.setDisplayTimeRange(start, end)

	def  __onSetGraph(self, event):
		# ????
		self.parent.clearCurrentGraphList()
		# ---------

		for i in range(self.inList.GetCount()) :
			graph_item = self.inList.GetString(i)
			graph_list = graph_item.split()
			type = BUSYGRAPH
			if graph_list[0] == "heatmap":
				type = HEATMAP
			elif graph_list[0] == "distance":
				type = DISTANCEGRAPH
			elif graph_list[0] == "vector":
				type = VECTORGRAPH
			elif graph_list[0] == "trend":
				type = TRENDGRAPH
			elif graph_list[0] == "window":
				type = WINDOWGRAPH
			elif graph_list[0] == "space":
				type = SPACEGRAPH
			elif graph_list[0] == "test":
				type = TESTGRAPH

			param1 = LOG_POINTER_MOVE
			if graph_list[1] == "window-new":
				param1 =  LOG_WINDOW_NEW
			elif graph_list[1] == "window-move":
				param1 =  LOG_WINDOW_CHANGE
			elif graph_list[1] == "vector":
				param1 = LOG_POINTER_VECTOR
			elif graph_list[1] == "active":
				param1 = LOG_POINTER_ACTIVE
			elif graph_list[1] == "click":
				param1 = LOG_POINTER_CLICK
			elif graph_list[1] == "lifetime":
				param1 = LOG_WINDOW_LIFETIME
			elif graph_list[1] == "distri-lifetime":
				param1 = LOG_WINDOW_DIS_TIME
			elif graph_list[1] == "size":
				param1 = LOG_WINDOW_SIZE
			elif graph_list[1] == "distri-size":
				param1 = LOG_WINDOW_DIS_SIZE
			elif graph_list[1] == "pointer-all":
				param1 = LOG_POINTER_MERGED
			elif graph_list[1] == "pointer-color":
				param1 = LOG_POINTER_COLOR
			elif graph_list[1] == "section":
				param1 = LOG_SECTION
			elif graph_list[1] == "section-setin":
				param1 = LOG_SECTION_DATAIN

			self.parent.setCurrentGraph(type, param1)

